import { ref, reactive, onMounted, nextTick, computed } from 'vue'
import { ExperiencePageApi, ExperienceDetailApi } from '@/api/Experience/ExperienceList'
import { EditType } from '@/type/BaseEnum'
import { DetailModel } from '@/api/01work/01type'
import { wandEditorDate } from '@/utils/index'
import { log } from 'console'

export default function tableModel() {
  const RefExperienceDetail = ref<{ show: (type: string, row?: DetailModel) => void }>();
  const tableDate = reactive({
    search: {
      page: 1,
      size: 10,
      createId: '',
      auditStatus: ''
    },
    radio1:false,
    Loading: false,
    list: [] as any,
    userInfo: {} as any,
    total: 0,
    statusId: '2',
    ExperienceList: [
      {
        id: '1',
        name: '我的经验库'
      },
      {
        id: '2',
        name: '全部经验库'
      }
    ],
    audiStatus: [
      {
        id: '0',
        name: '待审核'
      },
      {
        id: '1',
        name: '审核成功'
      },
      {
        id: '2',
        name: '审核驳回'
      }
    ],
    viewExperien: {} as any,
    searchParams: {} as any,
    fileId: '',
    tableHeight: 400,
  })
  onMounted(() => {
    tableDate.userInfo = JSON.parse(sessionStorage.getItem('UserInfoList') as any)
    getExperiencePage()
    nextTick(() => {
      tableDate.tableHeight = window.innerHeight - 185
    })
  })
  const getExperiencePage = async () => {
    tableDate.Loading = true
    const res = await ExperiencePageApi({ ...tableDate.search, ...tableDate.searchParams })
    if (res.code === 200) {
      // console.log(res.data);
      tableDate.list = res.data.records
      tableDate.total = res.data.total
      tableDate.Loading = false
    }
  }

  const ExperienceChange = (item) => {
    console.log(item);
    if (item == true) {
      tableDate.search.createId = tableDate.userInfo.id
      getExperiencePage()
    } else {
      tableDate.search.createId = ''
      getExperiencePage()
    }
  }
  const AuditStatusChange = async (item) => {
    tableDate.search.auditStatus = item
    tableDate.search.page=1
      getExperiencePage()
    // const res = await ExperiencePageApi({ ...tableDate.search, ...tableDate.searchParams })
    // console.log(res);
    // tableDate.list = res.data.records
  }
  const serachBtn = () => {
    tableDate.search.page = 1
    getExperiencePage()
  }
  const resetBtn = () => {
    tableDate.search.page = 1
    tableDate.searchParams = {}
    getExperiencePage()
  }
  const handleSizeChange = (size) => {
    tableDate.search.size = size
    getExperiencePage()

  }
  const handleCurrentChange = (page) => {
    tableDate.search.page = page
    getExperiencePage()
  }
  const tableRowClassName = ({ row, rowIndex }) => {
    if (rowIndex % 2 === 1) {
      return "warning-row";
    } else {
      return "success-row";
    }
  }
  const detailBtn = async (row) => {
    RefExperienceDetail.value?.show(EditType.View, row);
    console.log(RefExperienceDetail.value);

    const res = await ExperienceDetailApi({ id: row.id })
    if (res.code === 200) {
      nextTick(async () => {
        const newSt = await wandEditorDate(res.data.description)
        // RefWangEditor.value.valueHtml = newSt
        tableDate.viewExperien = { ...res.data, newSt: newSt }
      })
      // 单独处字段
    }
  }
  return {
    tableDate, serachBtn, resetBtn, getExperiencePage, handleSizeChange, handleCurrentChange, detailBtn, RefExperienceDetail, ExperienceChange, tableRowClassName,AuditStatusChange
  }
}